from random import *
from pymt import *
from pyglet.gl import *


from ConnectionGesture import *
from SelectionGesture import *

#class wire... represents a connection between MTConnectableObject     
class Wire:
    orig = MTConnectableObject(0,0)
    dest = MTConnectableObject(0,0)
    touchID = -1
    
    def __init__(self, orig, dest, touchID):
        self.orig = orig
        self.dest = dest
        self.touchID = touchID
        
    def on_draw (self, is_real):
        origX, origY = self.orig.calculateCenter()
        if is_real:
            destX, destY = self.dest.calculateCenter()
        else:    
            destX, destY = self.dest.x, self.dest.y
            
        drawLine ((origX, origY, destX, destY), 1)




class MTModifiedWindow(MTWindow):
    
    def __init__(self, **kwargs):
        super(MTModifiedWindow, self).__init__(**kwargs)
        
        #connection
        self.connectionGesture=ConnectionGesture()
        self.add_widget(self.connectionGesture)
        self.temp_connections=[]
        self.real_connections=[]
        
        #selection
        self.selectionGesture=SelectionGesture()
        self.add_widget(self.selectionGesture)
        
    
    def searchTempConnection(self, touchID):
        for w in self.temp_connections:
            if w.touchID == touchID:
                return w
        return None
    
    def on_draw(self):
        super(MTModifiedWindow, self).on_draw()
        glColor3d(1,1,1)

        #if there's any temp connection
        if len(self.temp_connections) > 0 :
            #for each one, draw it
            for wire in self.temp_connections:
                wire.on_draw(False)                
        
        #if there's any real connection
        if len(self.real_connections) > 0 :
            #for each one, draw it
            for wire in self.real_connections:
                wire.on_draw(True)
            
    
    def on_touch_down(self, touches, touchID, x, y): 
        super(MTModifiedWindow, self).on_touch_down(touches, touchID, x, y)
        for w in self.children:
            if isinstance(w, MTConnectableObject):
                if w.contains(x, y) and not w.selected:
                    self.temp_connections.append(Wire(w, MTConnectableObject(x, y), touchID))
                    return
    
    
    def on_touch_move(self, touches, touchID, x, y): 
        super(MTModifiedWindow, self).on_touch_move(touches, touchID, x, y)
        
        w = self.searchTempConnection(touchID)
        if w!=None:
            w.dest.x = x
            w.dest.y = y
    
    
    
    def on_touch_up(self, touches, touchID, x, y): 
        super(MTModifiedWindow, self).on_touch_up(touches, touchID, x, y)
        
        #iterates verifying if there is any temporary connection  
        w = self.searchTempConnection(touchID)
        
        #if w exists...
        if w!=None:
            #para cada caixa na tela
            for widget in self.children:
                #que seja do tipo conectavel
                if isinstance(widget, MTConnectableObject):
                    #se essa caixa contains o xy e nao e a fonte
                    if widget!=w.orig and widget.contains(x,y):
                        #adicione para ser desenhada
                        self.real_connections.append(Wire(w.orig, widget, touchID))    
            
            self.temp_connections.remove(w)
    
    
